如題,當初來會撰寫這篇參考筆記,主要也是因為,之前很剛好的看到了這篇教學文章:python PyEnchant(拼写检查),所以就有想到,或許可以利用 PyEnchant 套件,來重新製作一個類似之前寫過的 Scrabble Word Finder 專案的專案(好繞口w),簡單來說就是要根據鍵盤輸入所得到的英文字母來重新排列出一些可能有意義的單詞,因此就可以利用 PyEnchant 套件來快速的查找出有意義的英文單詞是哪些,然後因為這個程式碼是在八個月前隨意寫的,所以可能沒那麼好看,請見諒w。
然後程式碼 1 跟 2 的差異,就是有沒有使用到笛卡爾積 itertools.product
函式。
特此撰寫本篇文章作為紀錄文件,用以方便後續有需要的時候,可以快速的重複查閱,雖然後面比較沒有什麼機會再用到,但也算是一個還不錯的經驗。
使用 pip3 套件直接安装即可,指令如下:
pip3 install pyenchant
程式碼 version1,如下所示:
import enchant
import time
d = enchant.Dict("en_US")
input_words = input()
tStart = time.time()#計時開始
thirdwords = set(permutations(input_words,3))
# print(len(thirdwords))
fouthwords = set(permutations(input_words,4))
# print(len(fouthwords))
fifthwords = set(permutations(input_words,5))
# print(len(fifthwords))
sixthwords = set(permutations(input_words,6))
# print(len(sixthwords))
thirdprocessedwords = []
fouthprocessedwords = []
fifthprocessedwords = []
sixthprocessedwords = []
for thirdword in thirdwords:
tmpword = ''
for w in thirdword:
tmpword+=w
thirdprocessedwords.append(tmpword)
for fouthword in fouthwords:
tmpword = ''
for w in fouthword:
tmpword+=w
fouthprocessedwords.append(tmpword)
for fifthword in fifthwords:
tmpword = ''
for w in fifthword:
tmpword+=w
fifthprocessedwords.append(tmpword)
for sixthword in sixthwords:
tmpword = ''
for w in sixthword:
tmpword+=w
sixthprocessedwords.append(tmpword)
# print(thirdprocessedwords)
# print(len(thirdprocessedwords))
# print(fouthprocessedwords)
# print(len(fouthprocessedwords))
# print(fifthprocessedwords)
# print(len(fifthprocessedwords))
# print(sixthprocessedwords)
# print(len(sixthprocessedwords))
thirdprocessedwords = set(thirdprocessedwords)
fouthprocessedwords = set(fouthprocessedwords)
fifthprocessedwords = set(fifthprocessedwords)
sixthprocessedwords = set(sixthprocessedwords)
thirdcheckedwords = []
fouthcheckedwords = []
fifthcheckedwords = []
sixthcheckedwords = []
for word in thirdprocessedwords:
if d.check(word):
thirdcheckedwords.append(word)
for word in fouthprocessedwords:
if d.check(word):
fouthcheckedwords.append(word)
for word in fifthprocessedwords:
if d.check(word):
fifthcheckedwords.append(word)
for word in sixthprocessedwords:
if d.check(word):
sixthcheckedwords.append(word)
print('-'*100)
print('Three letters checked:',thirdcheckedwords)
print('Four letters checked:',fouthcheckedwords)
print('Five letters checked:',fifthcheckedwords)
print('Six letters checked:',sixthcheckedwords)
tEnd = time.time()#計時結束
print('It cost ',format(tEnd-tStart,'0.2f'),'second')
笛卡爾積:itertools.product(*iterables[, repeat])
程式碼 version2,如下所示:
from itertools import product
import enchant
import time
d = enchant.Dict("en_US")
input_words = input()
tStart = time.time()#計時開始
thirdwords = set(product(input_words, repeat = 3))
# print(len(thirdwords))
fouthwords = set(product(input_words, repeat = 4))
# print(len(fouthwords))
fifthwords = set(product(input_words, repeat = 5))
# print(len(fifthwords))
sixthwords = set(product(input_words, repeat = 6))
# print(len(sixthwords))
thirdprocessedwords = []
fouthprocessedwords = []
fifthprocessedwords = []
sixthprocessedwords = []
for thirdword in thirdwords:
tmpword = ''
for w in thirdword:
tmpword+=w
thirdprocessedwords.append(tmpword)
for fouthword in fouthwords:
tmpword = ''
for w in fouthword:
tmpword+=w
fouthprocessedwords.append(tmpword)
for fifthword in fifthwords:
tmpword = ''
for w in fifthword:
tmpword+=w
fifthprocessedwords.append(tmpword)
for sixthword in sixthwords:
tmpword = ''
for w in sixthword:
tmpword+=w
sixthprocessedwords.append(tmpword)
# print(thirdprocessedwords)
# print(len(thirdprocessedwords))
# print(fouthprocessedwords)
# print(len(fouthprocessedwords))
# print(fifthprocessedwords)
# print(len(fifthprocessedwords))
# print(sixthprocessedwords)
# print(len(sixthprocessedwords))
thirdprocessedwords = set(thirdprocessedwords)
fouthprocessedwords = set(fouthprocessedwords)
fifthprocessedwords = set(fifthprocessedwords)
sixthprocessedwords = set(sixthprocessedwords)
thirdcheckedwords = []
fouthcheckedwords = []
fifthcheckedwords = []
sixthcheckedwords = []
for word in thirdprocessedwords:
if d.check(word):
thirdcheckedwords.append(word)
for word in fouthprocessedwords:
if d.check(word):
fouthcheckedwords.append(word)
for word in fifthprocessedwords:
if d.check(word):
fifthcheckedwords.append(word)
for word in sixthprocessedwords:
if d.check(word):
sixthcheckedwords.append(word)
print('-'*100)
print('Three letters checked:',sorted(thirdcheckedwords))
print('Four letters checked:',sorted(fouthcheckedwords))
print('Five letters checked:',sorted(fifthcheckedwords))
print('Six letters checked:',sorted(sixthcheckedwords))
tEnd = time.time()#計時結束
print('It cost ',format(tEnd-tStart,'0.2f'),'second')